<?php
require_once("../shared/connect.php");

define('LOCK_TIMEOUT', 30);

// delete all locks older than LOCK_TIMEOUT seconds
function clear_old_locks() {
	$sql = "DELETE FROM `lock` WHERE last_modified < ".(time() - LOCK_TIMEOUT);
	mysql_query($sql) or die(mysql_error());
}

clear_old_locks();

$sql = "SELECT * FROM `lock` WHERE count > 0";
$query = mysql_query($sql) or die(mysql_error());

$out = "\"length\": ".mysql_num_rows($query).', "rows": { ';

while ($row = mysql_fetch_assoc($query)) {
	//$row[total_wait] += (time() - $row[start_time]);
	$out .= "\"$row[id]\": ".json_encode($row).",";
}

$out = substr_replace($out ,"",-1); // strip trailing comma. beware of case mysql_num_rows == 0

echo '{'.$out.'}}';

?>